package creation_patterns.simple_factory_pattern.factory;

import creation_patterns.simple_factory_pattern.abstract_product.Ball;
import creation_patterns.simple_factory_pattern.concrete_product.Basketball;
import creation_patterns.simple_factory_pattern.concrete_product.Football;
import creation_patterns.simple_factory_pattern.concrete_product.TableTennis;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;


public class CreateBallFactory {

    /**
     * 入参为class类型，可以省略String类型一一对应的维护工作
     * e.g. football -> Football.java
     * e.g. tabletennis -> TableTennis.java
    */
    public static Ball createBallBean(Class<? extends Ball> clazz) throws InstantiationException, IllegalAccessException {
        if(Objects.isNull(clazz)){
            throw new RuntimeException("参数不能为空!");
        }

        if(clazz.equals(Basketball.class)){
            // 或者可以通过反射创建对象
//           return clazz.newInstance();
            return new Basketball();
        }else if(clazz.equals(Football.class)){
            return new Football();
        }else if(clazz.equals(TableTennis.class)){
            return new TableTennis();
        }else {
            throw new RuntimeException("该工厂尚未支持当前类型的对象创建");
        }
    }
}
